import { Component } from '@angular/core';

import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { LocalNotifications, ELocalNotificationTriggerUnit } from '@ionic-native/local-notifications/ngx';

@Component({
  selector: 'app-tabs',
  templateUrl: 'tabs.page.html',
  styleUrls: ['tabs.page.scss']
})
export class TabsPage {
  showClock;
  nfid = 0;
  datetime;
  time = 1000 * 5;


  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private localNotifications: LocalNotifications,


  ) { }
  ngOnInit() {

    this.platform.ready().then(() => {
      setTimeout(() => {
        this.splashScreen.hide();
      }, 2000)

    });
    this.shownotification();
  }

  showcloclk() {
    // this.time = new Date(this.datetime).getTime()-new Date().getTime();
    console.log('开始');
    this.showClock = setInterval(() => {
      //实现倒计时的功能代码
      this.shownotification();
    }, this.time);
  }
  shownotification() {

    this.localNotifications.schedule([{
      id: 0,
      title: '日程提醒',
      text: "日程提醒0",
      icon: 'assets/img/logo.png',
      data: { secret: 'data' },
      vibrate: true,
      actions: [
        { id: 'yes', title: '关闭' },
        { id: 'no', title: '推迟5分钟' }
      ]
    }, {
      id: 1,
      title: '日程提醒',
      text: "日程提醒1",
      icon: 'assets/img/logo.png',
      data: { secret: 'data' },
      vibrate: true,
      actions: [
        { id: 'no', title: '推迟5分钟' },
        { id: 'yes', title: '关闭' },
      ]
    }]);
    this.localNotifications.on('yes').subscribe(result => {
      // alert('关闭'+result.id);
      this.localNotifications.cancel(result.id);
    });
    this.localNotifications.on('no').subscribe(result => {
      // alert('推迟5分钟后再次提醒'+result.id);
      this.NotidicationsPop(result);



    });
  }
  private NotidicationsPop(result: any) {
    var time = new Date(new Date().getTime() + 1000 * 60);
    this.localNotifications.schedule({
      id: result.id,
      title: result.title,
      text: "延迟通知" + result.text,
      icon: result.icon,
      data: { secret: 'data' },
      vibrate: true,
      trigger: { at: time },
      actions: [
        { id: 'no', title: '推迟5分钟' },
        { id: 'yes', title: '关闭' }
      ]
    });
  }

  stopClock() {
    clearInterval(this.showClock);

  }

}

